﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Models;
using BLL;

namespace Player.Admin
{
    public partial class game : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //刷新表
                rptgame_shuaxin();
                //下拉框绑定
                //添加游戏类型下拉框
                this.ddl_pl2type.DataSource = BLL.GmTypeManage.SeleAllGmType();
                this.ddl_pl2type.DataTextField = "Gt_Name";
                this.ddl_pl2type.DataValueField = "Gt_Id";
                this.ddl_pl2type.DataBind();
                //修改游戏类型下拉框
                this.ddl_pl2type2.DataSource = BLL.GmTypeManage.SeleAllGmType();
                this.ddl_pl2type2.DataTextField = "Gt_Name";
                this.ddl_pl2type2.DataValueField = "Gt_Id";
                this.ddl_pl2type2.DataBind();
                //修改
                //修改信息框展示
                if (Request.QueryString["upid"] != null)
                {
                    int upid = Convert.ToInt32(Request.QueryString["upid"]);
                    this.pl_deleupda.Visible = false;
                    this.Pl_update.Visible = true;
                    //给页面内容框赋值
                    List<Game> ls = BLL.GameManage.SeleIdGame(upid);
                    this.lbl_pl2gmid.Text = ls[0].Gm_Id.ToString();
                    this.txt_pl2name.Text = ls[0].Gm_Name;
                    this.ddl_pl2type.Items.FindByValue((ls[0].Gm_TypeId-48).ToString()).Selected = true;//根据value值显示对应文本
                    this.txt_pl2Price.Text = ls[0].Gm_Price.ToString();
                    this.txt_pl2imge.Text = ls[0].Gm_Image;
                    this.txt_pl2desc.Text = ls[0].Gm_Desc;
                    this.txt_pl2upcount.Text = ls[0].Gm_Upcount.ToString();
                    this.txt_pl2lv.Text = ls[0].Gm_Lv;
                }

                //删除
                if (Request.QueryString["deid"] != null)
                {
                    this.pl_delete.Visible = true;
                }
            }
        }

        //刷新表
        private void rptgame_shuaxin()
        {
            this.rptgame.DataSource = BLL.GameManage.SeleAllGame();
            this.rptgame.DataBind();
        }

        //添加信息框显示
        protected void btn_sele_Click(object sender, EventArgs e)
        {
            this.pl_deleupda.Visible = true;
            this.Pl_update.Visible = false;
            this.lbl_pltitle.Text = "添加游戏";
        }

        //添加信息框隐藏
        protected void btn_plgb_Click(object sender, EventArgs e)
        {
            this.pl_deleupda.Visible = false;
        }

        //保存添加用户
        protected void txt_plctrls_Click(object sender, EventArgs e)
        {
            //获取输入内容并保存到Game中
            Game gm = new Game();
            gm.Gm_Name = this.txt_plname.Text.Trim();
            gm.Gm_TypeId = Convert.ToInt32(this.ddl_pl2type2.SelectedValue);
            gm.Gm_Price = Convert.ToDouble(this.txt_plprice.Text.Trim());
            gm.Gm_Image = this.txt_plimage.Text.Trim();
            gm.Gm_Lv = this.txt_pllv.Text.Trim();
            gm.Gm_Desc = this.txt_pldesc.Text.Trim();
            gm.Gm_Upcount = Convert.ToInt32(this.txt_plupcount.Text.Trim());
            //执行添加
            bool pd = BLL.GameManage.AddGame(gm);
            if (pd)
            {
                this.lbl_pl1bcts.Text = "保存成功";
                //刷新表
                rptgame_shuaxin();
            }
            else
            {
                this.lbl_pl1bcts.Text = "保存失败";
            }

        }
        

        //修改信息框隐藏
        protected void btn_pl2gb_Click(object sender, EventArgs e)
        {
            this.Pl_update.Visible = false;
        }

        //保存修改信息
        protected void btn_pl2bc_Click(object sender, EventArgs e)
        {
            //获取内容保存至Game中
            Game gm = new Game();
            gm.Gm_Id = Convert.ToInt32(this.lbl_pl2gmid.Text);
            gm.Gm_Name = this.txt_pl2name.Text.Trim();
            gm.Gm_TypeId = Convert.ToInt32(this.ddl_pl2type.SelectedValue);
            gm.Gm_Price = Convert.ToDouble(this.txt_pl2Price.Text);
            gm.Gm_Image = this.txt_pl2imge.Text.Trim();
            gm.Gm_Lv = this.txt_pl2lv.Text.Trim();
            gm.Gm_Desc = this.txt_pl2desc.Text.Trim();
            gm.Gm_Upcount = Convert.ToInt32(this.txt_pl2upcount.Text.Trim());
            //修改操作
            bool pd = BLL.GameManage.UpdaGame(gm);
            if (pd)
            {
                this.lbl_pl2bcts.Text = "保存成功";
                rptgame_shuaxin();
            }
            else
            {
                this.lbl_pl2bcts.Text = "保存失败";
            }
        }

        //类型名称替换类型id
        public string GtIdisName(int id)
        {
            List<GmType> ls = BLL.GmTypeManage.SeleIdGmType(Convert.ToInt32(id));
            return ls[0].Gt_Name;
        }
        //删除提示框隐藏
        protected void btn_delequxiao_Click(object sender, EventArgs e)
        {
            this.pl_delete.Visible = false;
        }

        //确认删除
        protected void btn_delequedin_Click(object sender, EventArgs e)
        {
            int gmid = Convert.ToInt32(Request.QueryString["deid"]);
            bool pd1 = BLL.LiveManage.DeleGmIdLive(gmid);
            bool pd2 = BLL.CommentsManage.DeleGmIdComments(gmid);
            bool pd3 = BLL.OrdersManage.DeleGmIdOrders(gmid);
            bool pd4 = BLL.GameManage.DeleGame(gmid);

            if (pd4)
            {
                this.pl_delete.Visible = false;
                //刷新表
                rptgame_shuaxin();
            }
        }

        //刷新
        protected void imgbtnsx_Click(object sender, ImageClickEventArgs e)
        {
            //刷新表
            rptgame_shuaxin();
        }

        //查询
        protected void imgbtn_Click(object sender, ImageClickEventArgs e)
        {
            string name = this.txt_name.Text.Trim();
            if (name=="")
            {
                this.lbl_ChaXunTiShi.Text = "未找到相关内容";
                this.img_weizhaodao.Visible = true;
                return;
            }
            List<Game> ls=BLL.GameManage.SeleNameLikeGame(name);
            if (ls.Count==0)
            {
                this.lbl_ChaXunTiShi.Text = "未找到相关内容";
                this.img_weizhaodao.Visible = true;
                return;
            }
            this.lbl_ChaXunTiShi.Text = "";
            this.img_weizhaodao.Visible = false;
            this.rptgame.DataSource = ls;
            this.rptgame.DataBind();
        }
    
    }
}